package ro.prjmanager.web.actions.gen;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import ro.prjmanager.model.ComponentDetails;
import ro.prjmanager.model.GenericComponents;
import ro.prjmanager.model.Models;
import ro.prjmanager.model.TableSet;
import ro.prjmanager.service.CompGeneratorServiceInterface;
import ro.prjmanager.web.actions.GeneralAction;
import ro.prjmanager.web.annotation.Service;

import java.util.ArrayList;
import java.util.List;


@Service(target = "genService")
@SuppressWarnings("unchecked")
public class BulkGenerationAction extends GeneralAction {
    private List<Models> availableModels;
    private List<Integer> selectedModelIds;

    private List<GenericComponents> availableGenericComponents;
    private List<Integer> selectedGenericComponents;

    private List<ComponentDetails> availableComponentsDetails;
    private Integer selectedComponentsDetail;

    public List<Models> getAvailableModels() {
        return availableModels;
    }

    public void setAvailableModels(List<Models> availableModels) {
        this.availableModels = availableModels;
    }

    public List<Integer> getSelectedModelIds() {
        return selectedModelIds;
    }

    public void setSelectedModelIds(List<Integer> selectedModelIds) {
        this.selectedModelIds = selectedModelIds;
    }

    public List<GenericComponents> getAvailableGenericComponents() {
        return availableGenericComponents;
    }

    public void setAvailableGenericComponents(List<GenericComponents> availableGenericComponents) {
        this.availableGenericComponents = availableGenericComponents;
    }

    public List<Integer> getSelectedGenericComponents() {
        return selectedGenericComponents;
    }

    public void setSelectedGenericComponents(List<Integer> selectedGenericComponents) {
        this.selectedGenericComponents = selectedGenericComponents;
    }

    public List<ComponentDetails> getAvailableComponentsDetails() {
        return availableComponentsDetails;
    }

    public void setAvailableComponentsDetails(List<ComponentDetails> availableComponentsDetails) {
        this.availableComponentsDetails = availableComponentsDetails;
    }

    public Integer getSelectedComponentsDetail() {
        return selectedComponentsDetail;
    }

    public void setSelectedComponentsDetail(Integer selectedComponentsDetail) {
        this.selectedComponentsDetail = selectedComponentsDetail;
    }

    @HandlesEvent("showAdd")
    @DefaultHandler
    public Resolution showAdd(){
        List<TableSet> tableSetList = getService("tablesetService").list();
        if (tableSetList != null && !tableSetList.isEmpty()) {
            setAvailableModels(new ArrayList<Models>());
            for (TableSet tableSet : tableSetList) {
                if (tableSet instanceof Models)
                    getAvailableModels().add((Models) tableSet);
            }
        }

        setAvailableGenericComponents(getService("genericcomponentsService").list());
        setAvailableComponentsDetails(getService("componentdetailsService").list());


       return  new ForwardResolution("/pages/nomenclators/generate/masivGeneration.jsp");
    }


    @HandlesEvent("create")
    public Resolution generate(){
        CompGeneratorServiceInterface service= (CompGeneratorServiceInterface) getService();
        service.generate(selectedModelIds, selectedGenericComponents, selectedComponentsDetail);
       return new RedirectResolution("/index.jsp");
    }





}
